﻿/******************************************************************************
 * Name         : PacketFactoryManager.cpp
 * Author       : 671643387
 * Date         : 2015年9月21日
 * Description  :
 *****************************************************************************/

#include "EchoPacket.hpp"
#include "PacketFactoryManager.hpp"
using namespace sxr::common;

// 静态的对象
static PacketFactoryManager sPacketFactoryManager;
PacketFactoryManager* PacketFactoryManager::getMe()
{
    return &sPacketFactoryManager;
}

PacketFactoryManager::PacketFactoryManager()
{
	// 注册通用包
    addFactory(PacketFactoryPtr(new EchoPacketFactory()));
}

PacketFactoryManager::~PacketFactoryManager()
{

}

PacketPtr PacketFactoryManager::createPacket(int id)
{
    PacketFactoryPtr factory = factories_[id];
    if (factory)
    {
        return factory->create();
    }
    return PacketPtr();
}

void PacketFactoryManager::addFactory(PacketFactoryPtr factory)
{
    if (factories_[factory->getId()])
    {
        return;
    }

    factories_[factory->getId()] = factory;
}

void PacketFactoryManager::clean()
{
    factories_.clear();
}